package com.pinguix.games.pong;

import danger.app.Event;
import danger.app.Listener;
import danger.app.Timer;
import danger.system.Hardware;

public class LEDManager extends Listener
{

	private static Timer lightTimer;
	private static char blink = 0x0000;
	
	public LEDManager()
	{
		lightTimer = new Timer(175, false, this);
	}
	
	public static void activate()
	{
		new LEDManager();
	}
	
	public static void lightUpLeft()
	{
		Hardware.setLED(Hardware.LED_MOOD_TOP_LEFT, true);
		Hardware.setLED(Hardware.LED_MOOD_BOTTOM_LEFT, true);
		lightTimer.start();
		blink = 0x0001;
	}

	public static void lightUpRight()
	{
		Hardware.setLED(Hardware.LED_MOOD_TOP_RIGHT, true);
		Hardware.setLED(Hardware.LED_MOOD_BOTTOM_RIGHT, true);
		lightTimer.start();
		blink = 0x0100;
	}
	
	public static void lightsOff()
	{
		Hardware.setLED(Hardware.LED_MOOD_TOP_LEFT, false);
		Hardware.setLED(Hardware.LED_MOOD_BOTTOM_LEFT, false);
		Hardware.setLED(Hardware.LED_MOOD_TOP_RIGHT, false);
		Hardware.setLED(Hardware.LED_MOOD_BOTTOM_RIGHT, false);
		Hardware.setLED(Hardware.LED_TRACKBALL, false);
	}
	
	public boolean receiveEvent(Event e)
	{
		switch (e.type)
		{
			case Event.EVENT_TIMER:
				switch (blink)
				{
					case 0x0001: //Left off - 1 more flash
						Hardware.setLED(Hardware.LED_MOOD_TOP_LEFT, false);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_LEFT, false);
						lightTimer.start();
						blink = 0x0011;
						break;
					case 0x0011: //Left on
						Hardware.setLED(Hardware.LED_MOOD_TOP_LEFT, true);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_LEFT, true);
						lightTimer.start();
						blink = 0x0010;
						break;
					case 0x0010: //Left off -end
						Hardware.setLED(Hardware.LED_MOOD_TOP_LEFT, false);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_LEFT, false);
						break;
					case 0x0100: //Right off - 1 more flash
						Hardware.setLED(Hardware.LED_MOOD_TOP_RIGHT, false);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_RIGHT, false);
						lightTimer.start();
						blink = 0x1100;
						break;
					case 0x1100: //Right on
						Hardware.setLED(Hardware.LED_MOOD_TOP_RIGHT, true);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_RIGHT, true);
						lightTimer.start();
						blink = 0x1000;
						break;
					case 0x1000: //Right off - end
						Hardware.setLED(Hardware.LED_MOOD_TOP_RIGHT, false);
						Hardware.setLED(Hardware.LED_MOOD_BOTTOM_RIGHT, false);
						break;
				}
				break;
		}
		return (super.receiveEvent(e));
	}
}
